package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.EnumException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private LeaseAgreementService leaseAgreementService;



    @Override
    public void customPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customPage(page,queryVo);
    }

    @Override
    public void updateByApartmentSubmitVo(RoomSubmitVo roomSubmitVo) {
        //1.更新主表
        roomInfoMapper.updateById(roomSubmitVo);
        //2.删除关联表
        deleteOtherTablesById(roomSubmitVo.getId());
        //3.添加关联表
        addOtherTablesById(roomSubmitVo);
    }

    @Override
    public void saveByApartmentSubmitVo(RoomSubmitVo roomSubmitVo) {
        //1.保存主表
        roomInfoMapper.insert(roomSubmitVo);
        //2.添加关联表
        addOtherTablesById(roomSubmitVo);

    }

    @Override
    public RoomDetailVo getRoomDetailVoById(Long id) {

        return roomInfoMapper.selectByRoomId(id);
    }

    @Override
    public void removeByRoomId(Long id) {
        //租约状态（1:签约待确认，2:已签约，3:已取消，4:已到期，5:退租待确认，6:已退租，7:续约待确认）
        //如果是2，5，7 不能删，其他随意
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getRoomId,id);
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getIsDeleted,0);
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getStatus,2)
                .or()
                .eq(LeaseAgreement::getStatus,5)
                .or()
                .eq(LeaseAgreement::getStatus,7);
        long count = leaseAgreementService.count(leaseAgreementLambdaQueryWrapper);
        if (count>0){
            //如果count>0，证明租约状态在有效期不能退租
            throw new EnumException(ResultCodeEnum.DELETE_ERROR.getMessage(),ResultCodeEnum.DELETE_ERROR.getCode());
        }
        //删除主表
        roomInfoMapper.deleteById(id);
        //删除关系表
        deleteOtherTablesById(id);
    }

    /**
     * todo 根据roomSubmitVo添加关联表
     *
     */
    public void addOtherTablesById(RoomSubmitVo roomSubmitVo){
        Date date = new Date();
        graphInfoMapper.saveByRoomSubmitVo(roomSubmitVo.getId(),roomSubmitVo.getGraphVoList(),date);
        roomAttrValueMapper.saveByRoomSubmitVo(roomSubmitVo.getId(),roomSubmitVo.getAttrValueIds(),date);
        roomFacilityMapper.saveByRoomSubmitVo(roomSubmitVo.getId(),roomSubmitVo.getFacilityInfoIds(),date);
        roomLabelMapper.saveByRoomSubmitVo(roomSubmitVo.getId(),roomSubmitVo.getLabelInfoIds(),date);
        roomPaymentTypeMapper.saveByRoomSubmitVo(roomSubmitVo.getId(),roomSubmitVo.getPaymentTypeIds(),date);
        roomLeaseTermMapper.saveByRoomSubmitVo(roomSubmitVo.getId(),roomSubmitVo.getLeaseTermIds(),date);
    }
    /**
     * todo 根据主表id删除关联表
     */
    public void deleteOtherTablesById(Long id){
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //2代表图片
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,2);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getIsDeleted,0);
        graphInfoMapper.delete(graphInfoLambdaQueryWrapper);

        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getIsDeleted,0);
        roomAttrValueMapper.delete(roomAttrValueLambdaQueryWrapper);

        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getIsDeleted,0);
        roomFacilityMapper.delete(roomFacilityLambdaQueryWrapper);

        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getIsDeleted,0);
        roomLabelMapper.delete(roomLabelLambdaQueryWrapper);

        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getIsDeleted,0);
        roomPaymentTypeMapper.delete(roomPaymentTypeLambdaQueryWrapper);

        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getIsDeleted,0);
        roomLeaseTermMapper.delete(roomLeaseTermLambdaQueryWrapper);

    }


}




